package io2.bio;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class BioServer {
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(8080);
        System.out.println("服务启动 8080");
        while(true) {
            final Socket socket = serverSocket.accept();
            if(socket != null) {
                System.out.println("新建连接：" + socket.getRemoteSocketAddress());
                BufferedInputStream bufferedInputStream;
                try {
                    bufferedInputStream = new BufferedInputStream(socket.getInputStream());
                    byte[] bytes = new byte[1024];
                    int n;
                    while((n = bufferedInputStream.read(bytes)) > 0)
                        System.out.println("收到数据：" + new String(bytes, 0, n));
                    System.out.println("数据接收完毕，数据来自：" + socket.getRemoteSocketAddress());
//                    socket.getOutputStream().write("HTTP/1.1 200 OK\r\nContent-length:13\r\n\r\nHello, World!\n".getBytes());
//                    socket.getOutputStream().flush();
//                    System.out.println("响应数据发送完毕");
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        socket.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}
